package dataBase;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class CodeCategoriesTable extends SqlTable {

	public CodeCategoriesTable(String tableName,
			List<ColumnValue> schema, String engine) {
		super(tableName, schema, engine);
	}


	public List<String> getCIDbyCategories(Statement stmt, String query) {
		if ((stmt == null) || (query == null)){
			return null;
		}
		String searchQuery = "SELECT cid FROM " + tableName + " WHERE MATCH(category) AGAINST('" + query + "' IN BOOLEAN MODE);";
		try {
			ResultSet rs = stmt.executeQuery(searchQuery);
			List<String> result = new ArrayList<String>();
			while(rs.next()){
				result.add((String)rs.getObject("cid"));
			}
			return result;
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}	
	}


	public List<String> getCategories(Statement stmt, String cid) {
		if ((stmt == null) || (cid == null)){
			return null;
		}
		List<String> res = new LinkedList<String>();
		String query = "SELECT DISTINCT category FROM " + tableName + " WHERE cid ='" + cid + "';";
		try{
			ResultSet rs = stmt.executeQuery(query);
			while (rs.next()){
				res.add(rs.getString("category"));
			}
			return res;
		} catch(SQLException exception){
			return null;
		}
	}
}
